#include "mainwindow.h"
#include "ui_mainwindow.h"

#define POINT_STREAM_NUM 1 //数据流对象数量，对应曲线数量

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    plotArea = new PlotArea;
    streamList = new QList<PointStream *>();
    for(int i=0; i<POINT_STREAM_NUM; i++) {
        PointStream *stream = new PointStream();
        stream->setSamplesPerSeconds(20000/SAMPLE_GENERATION_PERIOD);
        *streamList << stream;
        plotArea->addPointGraphMap(stream);
    }
    thread = new CLineThread(streamList,parent);
    plotArea->setAxisRange(4.0);
    connect(thread,SIGNAL(updatePlot()),this,SLOT(update()));

    setCentralWidget(plotArea);
    thread->start();
#ifdef USE_TIMER
    startTimer(0.002);
#endif

}

MainWindow::~MainWindow()
{
    delete ui;
    plotArea = NULL;
    delete plotArea;
    thread->quit();

    streamList->clear();

    qDeleteAll(*streamList);

}
void MainWindow::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event)

    const double sineFrequency = 2.0; // 2Hz
    const double phase = 0.0;

    for(int i=0; i<streamList->size(); i++) {
        double value = qSin(2.0*M_PI*sineFrequency*sampleNumber*(((double)SAMPLE_GENERATION_PERIOD/1000.0) ));
        qDebug() << sampleNumber << value;
        QPointF data(sampleNumber, value);
        streamList->at(i)->appendPoints(data);
    }

    emit updatePlot();
    sampleNumber++;
}

void MainWindow::update()
{
    plotArea->update();
}
